<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form id="formId" onsubmit="return false" method="post">
		用户名:<input type="text" name="name"/><br/>
		密码:<input type="text" name="password"/><br/>
		邮箱:<input type="text" name="email"/><br/>
		部门<select id="selectId" name="department_id"></select><br/>
		年龄:<input type="text" name="age"/><br/>
		入职日期:<input id="joindate" type="text" name="joindate"/><br/>
		<button onclick="add()">新增</button>
	</form>
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js" ></script>
	<script src="<%=request.getContextPath()%>/static/lib/jquery/jquery-1.11.1.js"></script>
	<script src="${pageContext.request.contextPath}/static/lib/layui/layui.js" type="text/javascript" charset="utf-8"></script>
	<script src="<%=request.getContextPath()%>/static/common/mylayer.js"></script>
	<script type="text/javascript">
		layui.use(['layer','laydate'],function(){
			var table = layui.table;
			var laydate = layui.laydate;
			
			laydate.render({
			      elem: '#joindate'
			    });
		})
		$(function(){
			$.ajax({
				type : "POST",
				dataType : "json",
				url : "${pageContext.request.contextPath}/user/getDepartmentSelect.action",
				success  : function(resp){
					console.log(resp);
					var jsonObj = resp.data;
					console.log(jsonObj);
					var html = "<option>--请选择--</option>";
					for(var i=0;i<jsonObj.length;i++){
						html += "<option value='"+jsonObj[i].id+"'>"+jsonObj[i].name+"</option>";
					}
					$("#selectId").html(html);
				}
			})
		})
		function add(){
			$.ajax({
				type : "POST",
				dataType : "json",
				data : $("#formId").serialize(),
				url : "${pageContext.request.contextPath}/user/addUser.action",
				success : function(resp){
					console.log(resp)
					if(resp.code == 0){
						mylayer.success(resp.msg);
						var index = parent.layer.getFrameIndex(window.name);
			  			  //定时一秒后执行
			              setTimeout(function() {
			            	  //关闭当前弹出层
			                  parent.layer.close(index);
			                  //刷新父页面,刷新table
			                  window.parent.location.reload();
			               }, 1000);
					} else {
						mylayer.error(resp.msg);
					}
				}
			})
		}
	</script>
</body>
</html>